package com.hzlx.service.impl;/*
 *@author liu
 *2024/07/2024/7/25
 *@version
 */

import com.alibaba.fastjson.JSON;
import com.hzlx.common.ResponseBean;
import com.hzlx.entity.AccountFlow;
import com.hzlx.entity.CustomerAccount;
import com.hzlx.entity.CustomerInfo;
import com.hzlx.entity.WatchList;
import com.hzlx.mapper.AccountFlowMapper;
import com.hzlx.mapper.CustomerAccountMapper;
import com.hzlx.mapper.CustomerInfoMapper;
import com.hzlx.mapper.WithdrawalRecordsMapper;
import com.hzlx.service.AccountFlowService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author UP
 * @Date 2024/7/25 9:09
 * @Version 1.0
 **/

@Service
public class AccountFlowServiceImpl implements AccountFlowService {

    @Autowired
    private AccountFlowMapper accountFlowMapper;
    @Autowired
    private WithdrawalRecordsMapper withdrawalRecordsMapper;
    @Autowired
    private CustomerInfoMapper customerInfoMapper;
    @Autowired
    private CustomerAccountMapper customerAccountMapper;

    @Value("${session.key}")
    private String sessionKey;

    @Override
    public ResponseBean getAll(Integer pageIndex, Integer pageSize) {
        //1.判空
        if (pageSize == null || pageIndex == null) {
            pageSize = 5;
            pageIndex = 1;
        }

        //2.计算起始页
        Integer startIndex = (pageIndex - 1) * pageSize;
        //3.计算所有条数
        Integer count = accountFlowMapper.countGetAll(startIndex, pageSize);
        // 计算总页数
        Integer total = (count) % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        //4.获取本业数据并返回
        List<WatchList> watchVOList = accountFlowMapper.getAll(startIndex, pageSize);
        if (watchVOList != null) {
            return new ResponseBean(0, "success", watchVOList, total);
        }
        return new ResponseBean(10011, "获取失败", null);
    }

    @Override
    public ResponseBean getById(Integer id, String op, HttpSession session) {
        if (id == null) {
            return new ResponseBean(10011, "获取参数异常", null);
        }
        WatchList watchList = accountFlowMapper.getById(id);
        if (watchList != null) {
            session.setAttribute("op", op);
            session.setAttribute("statue",watchList.getStatus());
            session.setAttribute("watchVO", watchList);
            session.setAttribute("watchStr", JSON.toJSONString(watchList));
        }
        return new ResponseBean(0, "success", watchList);
    }

    @Override
    @Transactional
    public ResponseBean updateWatch(WatchList watchVO,String refuse) {
        if (watchVO.getStatus() == 1) {
            watchVO.setSuccessTime(new Date());
            //同意提现
            //1.扣除客户账户余额
            //获取客户信息，根据手机号获取
            CustomerInfo customerInfo = customerInfoMapper.getByTel(watchVO.getTelNumber());
            int up = customerInfoMapper.updateBalance(watchVO.getAmount(), customerInfo.getId());
            //2.修改提现记录表
            withdrawalRecordsMapper.updateStatuesById(watchVO.getId(), watchVO.getStatus(),null,watchVO.getVoucher());
            //3.新增账户流水记录
            AccountFlow accountFlow = new AccountFlow();
            //设置客户id
            accountFlow.setAccountId(customerInfo.getId());
            //获取交易前金额 根据客户Id获取客户余额
            CustomerAccount customerAccount = customerAccountMapper.getBalanceByCustomerId(customerInfo.getId());
            accountFlow.setBeforeBalance(customerAccount.getBalance());
            //交易金额
            accountFlow.setMoney(watchVO.getAmount());
            //交易后金额
            accountFlow.setAfterBalance(customerAccount.getBalance().subtract(watchVO.getAmount()));
            accountFlow.setType(3);
            //入住流水id
            accountFlow.setCheckInFlowNo(String.valueOf(System.currentTimeMillis()));
            //提现记录id
            accountFlow.setWithdrawalNo(watchVO.getFlowNo());
            accountFlowMapper.saveAccountFlow(accountFlow);
            return new ResponseBean(0, "提现成功", null);
        } else if (watchVO.getStatus() == 2) {
            //拒绝提现
            //修改客户提现记录表的状态 修改拒绝理由
            withdrawalRecordsMapper.updateStatuesById(watchVO.getId(), watchVO.getStatus(),refuse,null);
            return new ResponseBean(0, "提现失败", null);

        } else {
            return new ResponseBean(10011, "未指定审核结果", null);
        }
    }

    private String formatData(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
}
